#include "reward.h"
#include "ui_reward.h"
#include <QDebug>

Reward::Reward(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Reward),
    model(new CustomSqlModel(this))
{
    ui->setupUi(this);

    model->setTable("reward");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    model->setHeaderData(0, Qt::Horizontal, tr("name"));
    model->setHeaderData(1, Qt::Horizontal, tr("record"));

    ui->tableView->setModel(model);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

Reward::~Reward()
{
    delete ui;
}

void Reward::on_addButton_clicked()
{
    bool result  = model->insertRows(0, 1);
    if (!result)
    {
        qDebug() << "add fail!" << endl;
    }
}

void Reward::on_submitButton_clicked()
{
    model->submitAll();
}

void Reward::on_revertButton_clicked()
{
    model->revertAll();
}

void Reward::on_deleteButton_clicked()
{
    auto selection = ui->tableView->selectionModel();
    if (selection)
    {
        auto indexList = selection->selectedIndexes();
        for (auto& index : indexList)
        {
            model->removeRow(index.row());
        }
    }
}
